package com.shire.shire_interface.service.account.impl;

import com.shire.shire_interface.common.utils.FileUtils;
import com.shire.shire_interface.common.utils.JsonFormat;
import com.shire.shire_interface.common.utils.StrUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import java.io.IOException;
import java.net.URL;
import java.text.DecimalFormat;
import java.util.*;

@Service
public class LocationService {

    protected Logger logger = LoggerFactory.getLogger(this.getClass());
    private static List<Map<String, Object>> _locations = new ArrayList<Map<String,Object>>();
    private static String _city = "北京,天津,上海,广州,南京,杭州,重庆,武汉,深圳";
    private final static String _default_location = "39.92,116.46";
    private static Map<String, Map<String, Object>> _cityMap = new HashMap<String, Map<String,Object>>();

    @PostConstruct
    public void init(){
        load();
    }

    private void load(){
        try{
            URL url = LocationService.class.getClassLoader().getResource("location.json");
            String path = url.getPath();
            System.out.println("path = " + path);
            String content = FileUtils.readFileToString(this.getClass().getResourceAsStream("/location.json")).toString();

            List<Object> list = JsonFormat.toObject(content, ArrayList.class);
            List<Map<String, Object>> tmpList = new ArrayList<Map<String,Object>>();
            for(Object obj : list){
                Map<String, Object> map = (Map<String, Object>)obj;
                tmpList.add(map);
                List<Map<String, Object>> children = (List<Map<String, Object>>)map.get("children");
                for(Map<String, Object> son : children){
                    _cityMap.put((String)son.get("name"), son);
                }
            }
            _locations = tmpList;
        }
        catch(Exception e){
            logger.error("载入或解析经纬度文件异常[location.json]：", e);
        }
    }

    public String getRandomJsonLocation(){
        if(_locations.size()==0) return _default_location;
        Random random = new Random();
        int index = random.nextInt(_locations.size());
        Map<String, Object> prov = _locations.get(index);
        List<Map<String, Object>> children = (List<Map<String, Object>>)prov.get("children");
        if(children==null || children.size()==0){
            return _default_location;
        }
        random = new Random();
        index = random.nextInt(children.size());
        Map<String, Object> son = children.get(index);
        return getRandomJsonLocation(son);
    }

    public String getSpecCityLocation(String city){
        Map<String, Object> son = _cityMap.get(city);
        if(son!=null){
            return getRandomJsonLocation(son);
        }
        return "";
    }

    private String getRandomJsonLocation(Map<String, Object> son){
        Random random = new Random();
        float lat = StrUtils.getFloatValue((String)son.get("lat"), 39.92f);
        float log = StrUtils.getFloatValue((String)son.get("log"), 116.46f);
        int latiOffset = random.nextInt(100);
        int longtiOffset = random.nextInt(80);
        float lati = lat + ((latiOffset/1000.0f) *(latiOffset%2==0?1:-1));
        float longti = log + ((longtiOffset/1000.0f) *(longtiOffset%2==0?1:-1));

        DecimalFormat decimalFormat = new DecimalFormat(".000");
        String rst = decimalFormat.format(lati)+","+decimalFormat.format(longti);
//		System.out.println("["+son.get("name")+"][rst="+rst+"]["+son.get("lat")+","+son.get("log")+"], ("+latiOffset+","+longtiOffset+")");
        return rst;
    }

    public static void main(String[] args) throws IOException {
        try {

        }
        catch(Exception e){
            e.printStackTrace();
        }
    }
}
